package org.teamh.logic.datamodel;

import java.util.HashMap;

import org.teamh.game.Start;
import org.teamh.gui.ingame.data_structure.SafeList;

public class DatamodelManager {

	private Start start;
	private HashMap<String, Cell> cells = new HashMap<String, Cell>();
	private HashMap<String, CreepTypeToCreepMapping> creepTypeToCreepMappings = new HashMap<String, CreepTypeToCreepMapping>();
	private HashMap<String, CreepDescription> creepDescriptions = new HashMap<String, CreepDescription>();
	private HashMap<String, TowerTypeToTowerMapping> towerTypeToTowerMappings = new HashMap<String, TowerTypeToTowerMapping>();
	private HashMap<String, TowerDescription> towerDescriptions = new HashMap<String, TowerDescription>();
	private HashMap<String, Tile> tiles = new HashMap<String, Tile>();
	private HashMap<String, Map> maps = new HashMap<String, Map>();
	private HashMap<String, Waypoint> waypoints = new HashMap<String, Waypoint>();
	private HashMap<String, UserAssets> userAssetsMap = new HashMap<String, UserAssets>();
	private HashMap<String, User> users = new HashMap<String, User>();
	private HashMap<String, Team> teams = new HashMap<String, Team>();
	private SafeList<Tower> towers = new SafeList<Tower>();
	private SafeList<Creep> creeps = new SafeList<Creep>();
	
	public DatamodelManager(Start start) {
		this.setStart(start);
	}
	
	public void clearDataModel() {
		start.getLogic().setGame(null);
		cells.clear();
		creepTypeToCreepMappings.clear();
		creepDescriptions.clear();
		towerTypeToTowerMappings.clear();
		towerDescriptions.clear();
		tiles.clear();
		maps.clear();
		waypoints.clear();
		userAssetsMap.clear();
		users.clear();
		teams.clear();
		towers.clear();
		creeps.clear();
	}
	
	public void addCell(Cell cell) {
		cells.put(cell.getID(), cell);
	}
	
	public void removeCell(String ID) {
		cells.remove(ID);
	}
	
	public Cell getCell(String ID) {
		return cells.get(ID);
	}
	
	public void addCreepMapping(CreepTypeToCreepMapping creepMapping) {
		creepTypeToCreepMappings.put(creepMapping.getID(), creepMapping);
	}
	
	public void removeCreepMapping(String ID) {
		creepTypeToCreepMappings.remove(ID);
	}
	
	public CreepTypeToCreepMapping getCreepMapping(String ID) {
		return creepTypeToCreepMappings.get(ID);
	}
	
	public void addCreepDescription(CreepDescription creepDescription) {
		creepDescriptions.put(creepDescription.getID(), creepDescription);
	}
	
	public void removeCreepDescription(String ID) {
		creepDescriptions.remove(ID);
	}
	
	public CreepDescription getCreepDescription(String ID) {
		return creepDescriptions.get(ID);
	}
	
	public void addTowerMapping(TowerTypeToTowerMapping towerMapping) {
		towerTypeToTowerMappings.put(towerMapping.getID(), towerMapping);
	}
	
	public void removeTowerMapping(String ID) {
		towerTypeToTowerMappings.remove(ID);
	}
	
	public TowerTypeToTowerMapping getTowerMapping(String ID) {
		return towerTypeToTowerMappings.get(ID);
	}
	
	public void addTowerDescription(TowerDescription towerDescription) {
		towerDescriptions.put(towerDescription.getID(), towerDescription);
	}
	
	public void removeTowerDescription(String ID) {
		towerDescriptions.remove(ID);
	}
	
	public TowerDescription getTowerDescription(String ID) {
		return towerDescriptions.get(ID);
	}
	
	public void addTile(Tile tile) {
		tiles.put(tile.getID(), tile);
	}
	
	public void removeTile(String ID) {
		tiles.remove(ID);
	}
	
	public Tile getTile(String ID) {
		return tiles.get(ID);
	}
	
	public void addMap(Map map) {
		maps.put(map.getID(), map);
	}
	
	public void removeMap(String ID) {
		maps.remove(ID);
	}
	
	public Map getMap(String ID) {
		return maps.get(ID);
	}
	
	public void addWaypoint(Waypoint waypoint) {
		waypoints.put(waypoint.getID(), waypoint);
	}
	
	public void removeWaypoint(String ID) {
		waypoints.remove(ID);
	}
	
	public Waypoint getWaypoint(String ID) {
		return waypoints.get(ID);
	}
	
	public void addUserAssets(UserAssets userAssets) {
		userAssetsMap.put(userAssets.getID(), userAssets);
	}
	
	public void removeUserAssets(String ID) {
		userAssetsMap.remove(ID);
	}
	
	public UserAssets getUserAssets(String ID) {
		return userAssetsMap.get(ID);
	}
	
	public void addUser(User user) {
		users.put(user.getID(), user);
	}
	
	public void removeUser(String ID) {
		users.remove(ID);
	}
	
	public User getUser(String ID) {
		return users.get(ID);
	}
	
	public void addTeam(Team team) {
		teams.put(team.getID(), team);
	}
	
	public void removeTeam(String ID) {
		teams.remove(ID);
	}
	
	public Team getTeam(String ID) {
		return teams.get(ID);
	}
	
	public void addTower(Tower tower) {
//		towers.put(tower.getID(), tower);
		towers.add(tower.getID(), tower);
	}
	
	public void removeTower(String ID) {
		towers.remove(ID);
	}
	
	public Tower getTower(String ID) {
		return towers.get(ID);
	}
	
	public void addCreep(Creep creep) {
//		creeps.put(creep.getID(), creep);
		creeps.add(creep.getID(), creep);
	}
	
	public void removeCreep(String ID) {
		creeps.remove(ID);
	}
	
	public Creep getCreep(String ID) {
		return creeps.get(ID);
	}
	
	public HashMap<String, Waypoint> getWaypoints() {
		return waypoints;
	}
	
	public HashMap<String, Cell> getCells() {
		return cells;
	}
	
	public HashMap<String, Tile> getTiles() {
		return tiles;
	}
	
	public HashMap<String, CreepTypeToCreepMapping> getCreepMapping() {
		return creepTypeToCreepMappings;
	}
	
	public HashMap<String, TowerTypeToTowerMapping> getTowerMapping() {
		return towerTypeToTowerMappings;
	}
	
	public HashMap<String, CreepDescription> getCreepDescriptions() {
		return creepDescriptions;
	}
	
	public HashMap<String, TowerDescription> getTowerDescriptions() {
		return towerDescriptions;
	}
	
	public HashMap<String, Map> getMaps() {
		return maps;
	}
	
	public HashMap<String, UserAssets> getUserAssets() {
		return userAssetsMap;
	}
	
	public HashMap<String, User> getUsers() {
		return users;
	}
	
	public HashMap<String, Team> getTeams() {
		return teams;
	}
	
//	public ConcurrentHashMap<String, Tower> getTowers() {
//		return towers;
//	}
	
	public SafeList<Tower> getTowers() {
		return towers;
	}
	
//	public HashMap<String, Creep> getCreeps() {
//		return creeps;
//	}
	
	public SafeList<Creep> getCreeps() {
		return creeps;
	}

	public Start getStart() {
		return start;
	}

	public void setStart(Start start) {
		this.start = start;
	}
	
	public Cell getByXandY (int x, int y) {
		Cell cell1 =  null;
		for (Cell cell : cells.values()) {
			if (cell.getX() == x && cell.getY() == y) {
				cell1 = cell;
				break;
			}
		}
		return cell1;
	}
	
	public Waypoint getWaypointByIndex(int index) {
		Waypoint w = null;
		for (Waypoint waypoint : waypoints.values()) {
			if (waypoint.getIndex() == index) {
				w = waypoint;
				break;
			}
		}
		return w;
	}
}
